package com.example.myapplication2;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class AddItemActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;
    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private EditText editText6;
    private EditText editText7;
    private Button button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item2);
        initView();
    }

    private void initView() {
        editText = (EditText) findViewById(R.id.editText);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editText6);
        editText7 = (EditText) findViewById(R.id.editText7);
        button5 = (Button) findViewById(R.id.button5);

        button5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name = editText.getText().toString();
        String itemDesc = editText3.getText().toString();
        String itemRemark = editText4.getText().toString();
        String price = editText5.getText().toString();
        String id = editText6.getText().toString();
        String time = editText7.getText().toString();
        Map<String,String>map = new HashMap<>();
        map.put("itemName",name);
        map.put("itemDesc",itemDesc);
        map.put("itemRemark",itemRemark);
        map.put("initPrice",price);
        map.put("kindId",id);
        map.put("avail",time);
        try {
            String result = OkHttpUtil.postRequest(
                    "http://172.18.85.254:8080/auction/api/items",map);
            System.out.println(result);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void submit() {
        // validate
        String editTextString = editText.getText().toString().trim();
        if (TextUtils.isEmpty(editTextString)) {
            Toast.makeText(this, "editTextString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText3String = editText3.getText().toString().trim();
        if (TextUtils.isEmpty(editText3String)) {
            Toast.makeText(this, "editText3String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText4String = editText4.getText().toString().trim();
        if (TextUtils.isEmpty(editText4String)) {
            Toast.makeText(this, "editText4String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText5String = editText5.getText().toString().trim();
        if (TextUtils.isEmpty(editText5String)) {
            Toast.makeText(this, "editText5String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText6String = editText6.getText().toString().trim();
        if (TextUtils.isEmpty(editText6String)) {
            Toast.makeText(this, "editText6String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText7String = editText7.getText().toString().trim();
        if (TextUtils.isEmpty(editText7String)) {
            Toast.makeText(this, "editText7String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
